﻿using System;

namespace AntBot
{
	public class Location
	{
		private const int MAX_SIZE = 65535;
		private const short LOG_MAX_SIZE_2 = 16;

		public int Row { get; set; }
		public int Col { get; set; }

		public int X { get { return Col; } set { Col = value; } }
		public int Y { get { return Row; } set { Row = value; } }

		public Location(int c, int r)
		{
			Col = c;
			Row = r;
		}

		public Location(int hashCode)
		{
			Col = hashCode & MAX_SIZE;
			Row = hashCode >> LOG_MAX_SIZE_2;
		}

		public static bool operator ==(Location loc1, Location loc2)
		{
			if (ReferenceEquals(loc1, loc2))
			{
				// handles if both are null as well as object identity
				return true;
			}

			if ((object)loc1 == null || (object)loc2 == null)
			{
				return false;
			}
			return (loc1.Col == loc2.Col) && (loc1.Row == loc2.Row);
		}

		public static bool operator !=(Location loc1, Location loc2)
		{
			return !(loc1 == loc2);
		}

		public override bool Equals(Object obj)
		{
			if (obj == null || GetType() != obj.GetType())
				return false;

			return this == (Location)obj;
		}

		public override int GetHashCode()
		{
			//return Row * MAX_SIZE + Col;
			return (Row << LOG_MAX_SIZE_2) | Col;
		}

		public override string ToString()
		{
			return "(" + Col + "," + Row + ")";
		}
	}
}